package com.ewanghuiju.app.ui.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.AddAddressContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.request.AddressRequestBean;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.presenter.my.AddAddressPresenter;
import com.ewanghuiju.app.util.AutoSoftUtils;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.ewanghuiju.app.widget.SwitchButton;
import com.gyf.immersionbar.ImmersionBar;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityConfig;
import com.lljjcoder.style.cityjd.JDCityPicker;
import com.muddzdev.styleabletoast.StyleableToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 添加地址页面
 * @time 2020/2/24
 */
public class AddAddressActivity extends BaseActivity<AddAddressPresenter> implements AddAddressContract.View {

    @BindView(R.id.et_name)
    EditTextWithDel etName;
    @BindView(R.id.et_phone_no)
    EditTextWithDel etPhoneNo;
    @BindView(R.id.et_address_details)
    EditTextWithDel etAddressDetails;
    @BindView(R.id.mSwitchButton)
    SwitchButton mSwitchButton;
    @BindView(R.id.tv_city)
    TextView tvCity;

    private String pcdText;
    private String mProvince;//省
    private String mCity;//市
    private String mCountry;//区

    private int addressId = -1;

    @Override
    protected int getLayout() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initEventAndData() {
        AddressListResponBean addressListResponBean = (AddressListResponBean) getIntent().getSerializableExtra(Constants.ADDRESS_INFO);
        if (addressListResponBean != null) {
            etName.setText(addressListResponBean.getName());
            etPhoneNo.setText(addressListResponBean.getMobile());
            tvCity.setText(addressListResponBean.getProvince() + addressListResponBean.getCity() + addressListResponBean.getCountry());
            etAddressDetails.setText(addressListResponBean.getDetail());
            mProvince = addressListResponBean.getProvince();
            mCity = addressListResponBean.getCity();
            mCountry = addressListResponBean.getCountry();
            if (addressListResponBean.getIs_default() == 1) {
                mSwitchButton.setChecked(true);
            } else {
                mSwitchButton.setChecked(false);
            }
            addressId = addressListResponBean.getId();
        }
    }

    @OnClick({R.id.layout_area, R.id.tv_lj_save})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.layout_area:
                AutoSoftUtils.hideInput(etName);
                //地区选择
                JDCityPicker cityPicker = new JDCityPicker();
                JDCityConfig jdCityConfig = new JDCityConfig.Builder().build();

                jdCityConfig.setShowType(JDCityConfig.ShowType.PRO_CITY_DIS);
                cityPicker.init(this);
                cityPicker.setConfig(jdCityConfig);
                cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
                    @Override
                    public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                        tvCity.setText(province.getName() + city.getName() + district.getName());
                        pcdText = province.getId() + "," + city.getId() + "," + district.getId();
//                        ToastUtil.shortShow("城市选择结果：\n" + province.getName() + "(" + province.getId() + ")\n"
//                                + city.getName() + "(" + city.getId() + ")\n"
//                                + district.getName() + "(" + district.getId() + ")");
                        mProvince = province.getName();
                        mCity = city.getName();
                        mCountry = district.getName();
                    }

                    @Override
                    public void onCancel() {
                    }
                });
                cityPicker.showCityPicker();
                break;
            case R.id.tv_lj_save:
                saveAddress();
                break;
            default:
                break;
        }
    }

    /**
     * 保存地址
     */
    private void saveAddress() {
        String nameStr = etName.getText().toString();
        String phonenoStr = etPhoneNo.getText().toString();
        String detailsStr = etAddressDetails.getText().toString();
//
        if (TextUtils.isEmpty(nameStr)) {
            StyleableToast.makeText(mContext, "请输入收件人姓名", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        if (TextUtils.isEmpty(phonenoStr)) {
            StyleableToast.makeText(mContext, "请输入手机号码", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        if (!StringUtil.isPhone(phonenoStr)) {
            StyleableToast.makeText(mContext, "请输入正确手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        if (TextUtils.isEmpty(mProvince) || TextUtils.isEmpty(mCity) || TextUtils.isEmpty(mCountry)) {
            StyleableToast.makeText(mContext, "请输入所在地区", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        if (TextUtils.isEmpty(detailsStr)) {
            StyleableToast.makeText(mContext, "请输入详细地址", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        LoadingDialogUtils.show(mContext);
        AddressRequestBean addressRequestBean = new AddressRequestBean();
        addressRequestBean.setName(nameStr.trim());
        addressRequestBean.setMobile(phonenoStr.trim());
        addressRequestBean.setProvince(mProvince);
        addressRequestBean.setCity(mCity);
        addressRequestBean.setCountry(mCountry);
        addressRequestBean.setDetail(detailsStr.trim());
        addressRequestBean.setIs_default(mSwitchButton.isChecked() ? "1" : "0");
        if (addressId == -1) {
            mPresenter.addData(addressRequestBean);
        } else {
            addressRequestBean.setId(addressId);
            mPresenter.editData(addressRequestBean);
        }
    }

    @Override
    protected int getTitleIntText() {
        return R.string.add_address_title;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(AddressListResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, addressId == -1 ? "地址添加成功" : "地址编辑成功", Toast.LENGTH_SHORT, R.style.mytoast).show();

        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_ADDRESS));
        onBackPressedSupport();
        //延迟1秒  待优化
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                RxBus.getDefault().post(new SendEvent("", Constants.TYPE_ADDRESS));
//                onBackPressedSupport();
//            }
//        }, 1000);
    }
}
